package lucid.hrms.management.race;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.webflow.action.MultiAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

@Component("raceAction")
public class RaceAction extends MultiAction {
	private static final Logger logger = LoggerFactory
			.getLogger(RaceAction.class);

	@Autowired
	private RaceService raceService;

	public Event saveRace(RequestContext context) {
		logger.debug("saveRace");
		try {
			Race race = (Race) context.getViewScope().get("race");
			race = raceService.saveRace(race);
			context.getViewScope().put("race", race);
			context.getMessageContext().addMessage(
					new MessageBuilder().info()
							.defaultText("Race has been saved successfully")
							.build());

			return success();
		} catch (Exception e) {
			logger.debug("Exception: " + e);
			context.getMessageContext().addMessage(
					new MessageBuilder()
							.error()
							.defaultText(
									"Failed to save Race record. Reason: "
											+ e.getMessage()).build());
			return error();
		}
	}
}
